package com.zn.web.controller;

import com.aliyun.oss.OSSClient;
import com.zn.entity.AppCustomer;
import com.zn.entity.BankInfo;
import com.zn.entity.CustomerBankInfo;
import com.zn.service.AppCustomerService;
import com.zn.service.CustomerBankInfoService;
import com.zn.util.DateUtils;
import com.zn.util.OSSUtil;
import com.zn.vo.Message;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.List;

@RestController
@RequestMapping("/bankInfo")
public class BankInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BankInfoController.class);
    @Autowired
    private CustomerBankInfoService bankInfoService;
    @Autowired
    private AppCustomerService appCustomerService;

    /**
     * 客户绑卡接口
     * @param customerBankInfo
     * @return
     */
    @RequestMapping(value = "/customerTiedCard",method = RequestMethod.POST)
    public ResponseEntity<Message> customerTiedCard(@RequestBody CustomerBankInfo customerBankInfo){
        try {
            boolean flag=isAllFieldNull(customerBankInfo);
            /**参数异常**/
            if(flag){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            /**参数长度校验**/
            if(customerBankInfo.getCardholderTel().length()>11){
                return ResponseEntity.ok(Message.PHONE_NUM_ERROR);
            }
            if(customerBankInfo.getBankNo().length()>19){
                return ResponseEntity.ok(Message.BANK_NUM_ERROR);
            }
            if(customerBankInfo.getCardholderIdCard().length()>18){
                return ResponseEntity.ok(Message.ID_CARD_ERROR);
            }
            ResponseEntity<Message> responseEntity=bankInfoService.customerBindBankCard(customerBankInfo);
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }
    /**
     * 客户解除绑卡
     * @return
     */
    @RequestMapping(value = "/unbindBankCard",method = RequestMethod.POST)
    public ResponseEntity<Message> unbindBankCard(@RequestBody CustomerBankInfo customerBankInfo) {
        try{
            if(StringUtils.isBlank(customerBankInfo.getCustomerId()) || StringUtils.isBlank(customerBankInfo.getBankInfoId())){
                ResponseEntity.ok(Message.ARGS_ERROR);
            }
//            AppCustomer appCustomer=appCustomerService.selectByPrimaryKey(customerBankInfo.getCustomerId());
//            if(appCustomer==null){
//                return ResponseEntity.ok(Message.CUSTOMER_NOT_EXIST);
//            }
            ResponseEntity<Message> responseEntity=bankInfoService.unbindBankCard(customerBankInfo);
            return responseEntity;
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 获取客户银行卡列表
     * @return
     */
    @RequestMapping(value = "/getcustomerBankInfoList",method = RequestMethod.POST)
    public ResponseEntity<Message> getcustomerBankInfoList(@RequestBody CustomerBankInfo customerBankInfo) {
        try{
            if(StringUtils.isBlank(customerBankInfo.getCustomerId()) || StringUtils.isBlank(customerBankInfo.getBankInfoId())){
                ResponseEntity.ok(Message.ARGS_ERROR);
            }
            AppCustomer appCustomer=appCustomerService.selectByPrimaryKey(customerBankInfo.getCustomerId());
            if(appCustomer==null){
                return ResponseEntity.ok(Message.CUSTOMER_NOT_EXIST);
            }
            List<CustomerBankInfo> customerBankInfoList=bankInfoService.selectByCustomerId(customerBankInfo.getCustomerId());
            return ResponseEntity.ok(Message.successData(customerBankInfoList));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }
    /**
     * 客户选中卡接口
     * @return
     */
    @RequestMapping(value = "/customerSelectBank",method = RequestMethod.POST)
    public ResponseEntity<Message> customerSelectBank(@RequestBody CustomerBankInfo customerBankInfo) {
        try{

            if(StringUtils.isBlank(customerBankInfo.getCustomerId()) || StringUtils.isBlank(customerBankInfo.getBankInfoId())){
                ResponseEntity.ok(Message.ARGS_ERROR);
            }
            AppCustomer appCustomer=appCustomerService.selectByPrimaryKey(customerBankInfo.getCustomerId());
            if(appCustomer==null){
                return ResponseEntity.ok(Message.CUSTOMER_NOT_EXIST);
            }
            bankInfoService.customerSelectBank(customerBankInfo);
            return ResponseEntity.ok(Message.REQ_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }
    /**
     * 获取银行卡列表
     * @return
     */
    @RequestMapping(value = "/getBankInfoList",method = RequestMethod.POST)
    public ResponseEntity<Message> getBankInfoList() {
        try{
            List<BankInfo> bankInfoList=bankInfoService.selectAllBankInfo();
            return ResponseEntity.ok(Message.successData(bankInfoList));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    //判断该对象是否: 返回ture表示所有属性为null  返回false表示不是所有属性都是null
    public static boolean isAllFieldNull(Object obj) throws Exception{
        Class stuCla = (Class) obj.getClass();// 得到类对象
        Field[] fs = stuCla.getDeclaredFields();//得到属性集合
        boolean flag = true;
        for (Field f : fs) {//遍历属性
            if(!"bankInfoId".equals(f.getName())){
                f.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
                Object val = f.get(obj);// 得到此属性的值
                if(val!=null) {//只要有1个属性不为空,那么就不是所有的属性值都为空
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }
}